﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data;


namespace ConcreteMappersAndObjects.Mappers
{
    public class SemestersDataMapper : AbstractDataMapper<int, Semester>
    {
        private IEntityMapper<CUSemesterKey, CUSemester> _cuSemMapper;

        public void SetCUSemesterMapper(IEntityMapper<CUSemesterKey, CUSemester> cuSemMapper)
        {
            _cuSemMapper = cuSemMapper;
        }
        protected override int GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return sdr.GetInt32(0);
        }

        protected override string DoGetAllStatement()
        {
            return "SELECT [ID], [Description], [Number] FROM [ExaminationTimetablingDB].[dbo].[Semester]";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + "WHERE [ID] = @key";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, int key)
        {
            cmd.Parameters.AddWithValue("@key", key);
        }

        private class SemFilter : IFilter
        {
            private int _semKey;
            public SemFilter(int key)
            {
                _semKey = key;
            }
            #region IFilter Members

            public string GetWhereClause()
            {
                return " where SemesterID = @SemesterID";
            }

            public void BindValues(System.Data.SqlClient.SqlCommand cmd)
            {
                cmd.Parameters.AddWithValue("@SemesterID", _semKey);
            }

            #endregion
        }


        protected override Semester DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            LazySet<CUSemesterKey, CUSemester> cunits = 
                new LazySet<CUSemesterKey,CUSemester>(_cuSemMapper, new SemFilter(GetKeyFrom(sdr)));
            return new Semester(GetKeyFrom(sdr), sdr.GetString(1), sdr.GetInt32(2), cunits);
        }

        protected override string GetUpdateCommand()
        {
            return
                "UPDATE [ExaminationTimetablingDB].[dbo].[Semester] SET [Description] = @Description, [Number] = @Number WHERE ID = @ID";
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, Semester t)
        {
            cmd.Parameters.AddWithValue("@ID", t.ID);
            cmd.Parameters.AddWithValue("@Description", t.Description);
            cmd.Parameters.AddWithValue("@Number", t.Number);
        }

        protected override string GetInsertCommand()
        {
            return "SemesterInsert";
        }

        protected override void DoBindInsert(System.Data.SqlClient.SqlCommand cmd, Semester t)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Description", t.Description);
            cmd.Parameters.AddWithValue("@Number", t.Number);
        }

        protected override string GetRemoveCommand()
        {
            return "DELETE FROM [Semester] WHERE ID = @ID";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, Semester t)
        {
            cmd.Parameters.AddWithValue("@ID", t.ID);
        }

        protected override void FinishUpdateObject(Semester t)
        {
            
        }

        protected override void FinishNewObject(Semester t, object id)
        {
            t.ID = Convert.ToInt32(id);
        }

        protected override void FinishDeleteObject(Semester t)
        {
     
        }
    }
}
